var config = {
  APPID: 'wx27900e3078720b6f',
  socialType: 'wx-jqfx',
  apiUrl: 'https://gate.97kid.com',
  staticUrl: 'https://gstatic.97kid.com',
  requestTimeout: 20000,
  appId: 'antYy9lOFi5mkU9QKjtYmRLM-gzGzoHsz',
  appKey: 'jqyWxLyobpsmdwsJddJlDheb'
}

function checkWxExist() {
  return typeof wx === 'object';
}

function WxConfig (options, campaign, random) {
  checkWxExist() && wx.config({
    debug: false,
    appId: config.APPID,
    timestamp: options.timestamp,
    nonceStr: options.nonceStr,
    signature: options.signature,
    jsApiList: options.jsApiList
  })

  checkWxExist() && wx.error(function (res) {
    console.log('微信验证失败 ' + JSON.stringify(res))
  })

  var shareMsg = {
    title: options.title || '【Qkids久趣】4-12岁，在家跟北美外教学英语，新用户100元体验！',
    desc: options.desc || '严选北美外教在线授课，创造趣味十足的课堂体验，让孩子爱学更有效。',
    link: options.link || window.location.protocol + '//' + window.location.host + '/wx',
    imgUrl: options.imgUrl || 'https://static-app.97kid.com/site-student/public/img/share.png',

    success: function () {},
    fail: function (res) {
      // $.toastAlert('微信分享朋友圈失败: ' + JSON.stringify(res));
    }
  }

  switch (campaign) {
    case 'doubleeleven':
      shareMsg.title = '【双11】久趣148元/8课时！在家跟北美外教学英语。'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/doubleeleven' + Math.ceil(Math.random() * 100)
      break
    case 'christmas':
      shareMsg.title = '【久趣英语圣诞主题课】9.9元和北美外教过圣诞！'
      shareMsg.desc = '4-12岁在线上课，4人成团。快来唱圣诞歌，体验地道圣诞文化！'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/christmas' + Math.ceil(Math.random() * 100)
      break
    case 'winterclass':
      shareMsg.title = '【久趣148元/10课时】寒假在家跟北美外教学英语'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/winterclass' + Math.ceil(Math.random() * 100)
      break
    case 'award':
      shareMsg.title = '【久趣特惠团购】给4-12岁儿童的北美外教超值英语课！'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/award' + Math.ceil(Math.random() * 100)
      break
    case 'easter':
      shareMsg.title = '【久趣英语复活节主题课】19.9元，北美外教带你画彩蛋！'
      shareMsg.desc = '4-12岁，四人成团，在线上课。快来体验复活节文化，get纯正美语！'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/easter' + Math.ceil(Math.random() * 100)
      break
    case 'easter1':
      shareMsg.title = '【久趣英语复活节主题课】29.9元，北美外教带你画彩蛋！'
      shareMsg.desc = '4-12岁，四人成团，在线上课。快来体验复活节文化，get纯正美语！'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/easter1' + Math.ceil(Math.random() * 100)
      break
    case 'phonics':
      shareMsg.title = '限时！久趣自然拼读，9元解锁2节课！'
      shareMsg.desc = '北美外教在线教学，让孩子轻松认读新单词！'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/phonics' + Math.ceil(Math.random() * 100)
      break
    case 'two':
      shareMsg.title = '【久趣五月2人班】￥360/8次课，严选北美外教在线上课'
      shareMsg.desc = '4-12岁适用，2人成团，严选北美外教在线上课，创造趣味十足的课堂体验。'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/two' + Math.ceil(Math.random() * 100)
      break
    case 'summer':
      shareMsg.title = '【久趣暑假第一波 |￥168/10节课，在家和北美外教学英语'
      shareMsg.desc = '4-12岁使用，4人成团，严选北美外教在线授课，创造趣味十足的课堂体验。'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/summer' + Math.ceil(Math.random() * 100)
      break
    case 'summers':
      shareMsg.title = '久趣8月福利|暑假第二波 ￥168/10节课'
      shareMsg.desc = '4-12岁使用，4人成团，在家和北美外教学英语。'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/summers' + Math.ceil(Math.random() * 100)
      break
    case 'summer2s':
      shareMsg.title = '久趣暑期特惠｜¥168/10节课，在家和北美外教学英语！'
      shareMsg.desc = '适合4-12岁，零基础的小朋友也可轻松学会！'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/summer2s' + Math.ceil(Math.random() * 100)
      break
    case 'summer3s':
      shareMsg.title = '久趣暑期特惠｜¥168/10节课，在家和北美外教学英语！'
      shareMsg.desc = '适合4-12岁，零基础的小朋友也可轻松学会！'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/summer3s' + Math.ceil(Math.random() * 100)
      break
    case 'insect':
      shareMsg.title = '19.9元4节「久趣昆虫主题课」北美外教带你探索"昆虫的秘密"！'
      shareMsg.desc = '4-12岁，三人成团，在线上课。一起走进奥妙的昆虫世界，get纯正美语！'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/insect' + Math.ceil(Math.random() * 100)
      break
    case 'invite':
      shareMsg.title = '送你1节孩子最爱的北美外教课，免费哟~'
      shareMsg.desc = '快来跟我们做同学吧~'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/invite' + Math.ceil(Math.random() * 100)
      break
    case 'lottery':
      shareMsg.title = '【久趣福利】1元抢3节北美外教课！'
      shareMsg.desc = '100%北美外教在线教学，让孩子爱上英语！'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/lottery' + Math.ceil(Math.random() * 100)
      shareMsg.imgUrl = 'https://static-app.97kid.com/site-student/public/mobile/campaign/lottery/img/share.jpg'
      break
    case '1v1':
      shareMsg.title = '【5折福利】久趣1对1北美精英外教在线教学，仅500名额！'
      shareMsg.desc = '适合4-12岁，专属外教固定时间，让孩子爱上英语！'
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/one' + Math.ceil(Math.random() * 100)
      shareMsg.imgUrl = 'https://static-app.97kid.com/site-student/public/mobile/campaign/1v1/img/share.jpg'
      break
    default:
      shareMsg.title = '【Qkids久趣】4-12岁，在家跟北美外教学英语，新用户100元体验！'
      shareMsg.desc = '严选北美外教在线授课，创造趣味十足的课堂体验，让孩子爱学更有效。'
      shareMsg.link = options.link || window.location.protocol + '//' + window.location.host + '/wx'
      break
  }

  function patchShareMsg (shareMsg, target) {
    var defaultSuccessFunction = shareMsg.success
    return {
      title: shareMsg.title,
      desc: shareMsg.desc,
      link: shareMsg.link,
      imgUrl: shareMsg.imgUrl,
      success: function () {
        if (window.AV && window.AV.analytics instanceof Function) {
          var appId = 'antYy9lOFi5mkU9QKjtYmRLM-gzGzoHsz'
          var appKey = 'jqyWxLyobpsmdwsJddJlDheb'

          var analytics = window.AV.analytics({
            appId: appId,
            appKey: appKey,
            channel: 'weixin'
          })
          analytics.send({
            event: 'share',
            attr: {
              url: shareMsg.link,
              target: target
            }
          })
        }
        if (defaultSuccessFunction && typeof defaultSuccessFunction == 'function') {
          defaultSuccessFunction.call(this, target)
        }
      },
      cancel: shareMsg.cancel
    }
  }

  //初始化分享接口
  checkWxExist() && wx.ready(function () {
    wx.onMenuShareTimeline(patchShareMsg(shareMsg, 'timeline'))
    wx.onMenuShareAppMessage(patchShareMsg(shareMsg, 'appMessage'))
    wx.onMenuShareQQ(patchShareMsg(shareMsg, 'QQ'))
    wx.onMenuShareQZone(patchShareMsg(shareMsg, 'QZone'))
  })

  //登陆用户分享接口
  WxConfig.prototype.activeShare = function (shareFlag, camParas, _shareMsg) {
    // campaign 将逐步弃用掉, 改成使用_shareMsg控制
    var extraParas = ''
    for (var para in camParas) {
      extraParas += '&' + para + '=' + camParas[para]
    }
    if (campaign !== undefined && campaign !== '') {
      if (random === undefined) random = true
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx/' + campaign
      if (random) {
        shareMsg.link += '/' + Math.ceil(Math.random() * 100)
      }
      shareMsg.link += '?_r=' + shareFlag + extraParas
    } else {
      shareMsg.link = window.location.protocol + '//' + window.location.host + '/wx?_r=' + shareFlag + extraParas
    }

    // 今后都将采用_shareMsg控制, _shareMsg将会覆盖上面的配置
    if (_shareMsg) {
      shareMsg = {
        title: _shareMsg.title || shareMsg.title,
        desc: _shareMsg.desc || shareMsg.desc,
        link: _shareMsg.link || shareMsg.link,
        imgUrl: _shareMsg.imgUrl || shareMsg.imgUrl,
        success: _shareMsg.success || shareMsg.success,
        fail: _shareMsg.fail || shareMsg.fail
      }
    }
    checkWxExist() && wx.ready(function () {
      wx.onMenuShareTimeline(patchShareMsg(shareMsg, 'timeline'))
      wx.onMenuShareAppMessage(patchShareMsg(shareMsg, 'appMessage'))
      wx.onMenuShareQQ(patchShareMsg(shareMsg, 'QQ'))
      wx.onMenuShareQZone(patchShareMsg(shareMsg, 'QZone'))
    })
  }

  WxConfig.prototype.pay = function (billId, pId, options, campUrl) {
    checkWxExist() && wx.ready(function () {
      wx.chooseWXPay({
        timestamp: options.timeStamp,
        nonceStr: options.nonceStr,
        package: options.package,
        signType: options.signType,
        paySign: options.paySign,

        // 支付成功后的回调函数
        success: function (res) {
          if (options.success && typeof options.success === 'function') {
            if (options.success(res)) return
          }
          if (options.redirect && typeof options.redirect === 'string') {
            if (options.redirect !== '') window.location.href = options.redirect
          } else {
            //是否为活动
            if (typeof(campUrl) !== 'undefined') {
              window.location.href = '/wx/verifypayment/' + pId + '?out_trade_no=' + billId + '&campUrl=' + campUrl
            } else {
              window.location.href = '/wx/verifypayment/' + pId + '?out_trade_no=' + billId
            }
          }
        },
        // 支付失败后的回调函数
        fail: function (res) {
          $.toastAlert('微信支付唤起失败: ' + JSON.stringify(res))
          if (options.fail || options.fail instanceof Function) options.fail(res)
        }
      })
    })
  }
}
